package org.dtrd.modules.achievement.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.achievement.entity.bean.PatientSigninPlan;
import org.dtrd.modules.achievement.entity.request.ChangeSigninPlanStatusRequest;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninPlanService;
import org.dtrd.modules.base.service.BaseCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_UPDATE;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @author qr
 * @date 2022/1/7 11:40
 */
@Api(tags = "打卡计划")
@RestController
@RequestMapping("signin/plan/")
@Slf4j
public class SigninPlanController extends BaseController {

    @Autowired
    private IDtrdEntRdSigninPlanService signinPlanService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "获取患者打卡计划", response = PatientSigninPlan.class)
    @GetMapping("get")
    public Result<?> getPatientSignIgPlan(@RequestParam Integer patientId){
        PatientSigninPlan signinPlan = signinPlanService.getSigninPlan(patientId);
        String logContent = String.format("获取患者打卡计划, 患者id: %d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return signinPlan == null ? error(STATE_NO_RECORD) : Result.OK(signinPlan);
    }

    @ApiOperation("更新患者打卡计划")
    @PostMapping("update")
    public Result<?> updatePatientSigninPlanContent(@RequestBody PatientSigninPlan signinPlan){
        Integer dataId = signinPlan.getDataId();
        if (isIntParamInvalid(dataId)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = signinPlanService.updatePatientSignInPlanContent(signinPlan);
        String logContent = String.format("更新患者打卡计划, 更新人: %d  ； 更新记录：%d", signinPlan.getPatientId(), signinPlan.getDataId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("调整患者打卡计划状态")
    @PostMapping("status/change")
    public Result<?> changePatientSigninPlanStatus(@RequestBody ChangeSigninPlanStatusRequest request){
        Integer patientId = request.getPatientId();
        Integer status = request.getStatus();
        if (isIntParamInvalid(patientId) || status == null) {
            return error(STATE_PARAMS_MISSING);
        }
        String remark = request.getRemark();
        if (StrUtil.isBlank(remark)) {
            return Result.error("备注不能为空");
        }
        if (remark.length() > Constant.REMARK_LIMIT) {
            return Result.error("备注不能多于100个字");
        }
        if (status != Constant.SignInPlanUsageStatus.Using.getType()
                && status != Constant.SignInPlanUsageStatus.Pause.getType()) {
            return error(STATE_PARAMS_ERROR);
        }
        boolean result = signinPlanService.updatePatientSignInPlanStatus(patientId, status, remark);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }


//    @ApiOperation("设置患者的打卡计划")
//    @PostMapping("patient/link")
//    public Result<?> savePatientSignInPlan(@RequestBody PatientSigninPlanRequest request){
//        Integer patientId = request.getPatientId();
//        Integer signInTemplateId = request.getSignInTemplateId();
//        if (isIntParamInvalid(patientId)
//                || isIntParamInvalid(signInTemplateId)) {
//            return error(STATE_PARAMS_MISSING);
//        }
//        String startTimeStr = request.getStartTime();
//        String endTimeStr = request.getEndTime();
//        Date startTime;
//        Date endTime;
//        try {
//            startTime = DateUtils.parseDate(startTimeStr, DateUtils.date_sdf.get().toPattern());
//            endTime = DateUtils.parseDate(endTimeStr, DateUtils.date_sdf.get().toPattern());
//        } catch (ParseException e) {
//            log.error("SigninPlanController.savePatientSignInPlan 设置患者打卡计划日期格式错误, {} 或 {}", startTimeStr, endTimeStr);
//            e.printStackTrace();
//            return error(STATE_PARAMS_ERROR);
//        }
//        boolean result = signinPlanService.saveOrUpdatePatientSignInPlan(patientId, signInTemplateId, startTime, endTime);
//        String logContent = String.format("设置患者的打卡计划, 患者id: %d, 打卡模版id: %d", patientId, signInTemplateId);
//        baseCommonService.addLog(logContent, OPERATE_TYPE_UPDATE);
//        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
//    }
}
